<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="dialogIsShow" destory-on-close="true" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addForm" label-width="120px" status-icon :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="checkmanager">
          <el-option
            v-for="user in employeesList"
            :key="user.id"
            :label="user.username"
            :value="user.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addDepartment">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartment, getDepartDetail, editDepartment } from '@/api/departments'
import { getEmployeesSimple } from '@/api/employee'
export default {
  props: {
    dialogIsShow: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    // 创建判断部门名称是否重复的校验函数
    const checkNameRepeat = async(rule, val, callback) => {
      // 创建一个常量保存是否重复的结果,根据这个结果来返回不同的回调函数
      const { depts } = await getDepartments()
      let isRepeat
      if (this.formData.id) {
        isRepeat = depts.filter(item => item.pid === this.formData.pid).some(item => item.name === val && item.id !== this.formData.id)
      } else {
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === val)
      }
      console.log(49, isRepeat)
      if (isRepeat) {
        callback(new Error('同一父部门下的部门名称不能相同'))
      } else {
        callback()
      }
    }
    // 创建判断部门编码是否重复的校验函数
    const checkCodeRepeat = async(rule, val, callback) => {
      const { depts } = await getDepartments()
      let codeRepeat
      if (this.formData.id) {
        codeRepeat = depts.some(item => item.code === val && item.id !== this.formData.id)
      } else {
        codeRepeat = depts.some(item => item.code === val)
      }
      codeRepeat ? callback(new Error('部门编码必须唯一')) : callback()
    }
    return {
      employeesList: [],
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNameRepeat, trigger: 'blur' }],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkCodeRepeat, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  async created() {
    this.employeesList = await getEmployeesSimple()
  },
  methods: {
    checkmanager() {
      setTimeout(() => {
        this.$refs.addForm.validateField('manager')
      }, 100)
    },
    async addDepartment() {
      // 校验表单
      await this.$refs.addForm.validate()
      // 发送添加请求
      if (this.formData.id) {
        // 编辑
        await editDepartment(this.formData)
        // 提示用户添加成功
        this.$message.success('更新部门信息成功!')
      } else {
        // 新增
        await addDepartment({
          ...this.formData,
          pid: this.treeNode.id
        })
        // 提示用户添加成功
        this.$message.success('添加部门成功!')
      }

      // 更新部门数据
      this.$emit('addDepartment')
      // 关闭弹窗
      this.$emit('update:dialogIsShow', false)
    },
    btnCancel() {
      // 清空表单验证
      this.$refs.addForm.resetFields()
      // 清空表单数据
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 关闭弹窗
      this.$emit('update:dialogIsShow', false)
    },
    async editDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style>

</style>
